
#include "user_password.h"

#if GKT_USER_PASSWORD_DYNAMIC_ENABLE && USER_DYNPWD_EDTQ_ENABLE

int user_dynpwd_EDTQ_retrieve(user_dynpwd_data_s *data,
				uint32_t *values)
{
	uint32_t year_offset, tag;

	data->edtq.st_hour = USER_DYNPWD_EDTQ_FIELD(values[0], ST_HOUR);
	if (data->edtq.st_hour >= GKT_DT_DAY_HOURS) {
		gkt_trace("user_dynpwd_edtq: start hour(%u) invalid!\n", 
			data->edtq.st_hour);
		return GKT_EPARAM;
	}

	data->edtq.et_hour = USER_DYNPWD_EDTQ_FIELD(values[0], ET_HOUR);
	if ((data->edtq.et_hour > GKT_DT_DAY_HOURS)
		|| (data->edtq.et_hour <= data->edtq.st_hour)) 
	{
		gkt_trace("user_dynpwd_edtq: start hour(%u) invalid!\n", 
			data->edtq.et_hour);
		return GKT_EPARAM;
	}

	year_offset = USER_DYNPWD_EDTQ_FIELD(values[0], ED_YEAR_OFFSET);
	if (year_offset >= USER_DYNPWD_EDTQ_ED_YEAR_OFFSET_MAX) {
		gkt_trace("user_dynpwd_edtq: year_offset(%u) invalid!\n", 
			year_offset);
		return GKT_EPARAM;
	}

	data->edtq.expire_date.day = USER_DYNPWD_EDTQ_FIELD(values[0], ED_DAY);
	data->edtq.expire_date.month = USER_DYNPWD_EDTQ_FIELD(values[0], ED_MONTH);
	data->edtq.expire_date.year = year_offset + USER_DYNPWD_BASE_YEAR;
	if (!gkt_dt_check_date(&data->edtq.expire_date)) {
		gkt_trace("user_dynpwd_edtq: expire_date(%04u-%02u-%02u) invalid!\n", 
				data->edtq.expire_date.year, data->edtq.expire_date.month,
				data->edtq.expire_date.day);
		return GKT_EPARAM;
	}

	tag = USER_DYNPWD_EDTQ_FIELD(values[0], TAG);
	if (tag != USER_DYNPWD_EDTQ_TAG_VALUE) {
		gkt_trace("user_dynpwd_edtq: tag(%u) invalid!\n", tag);
		return GKT_EPARAM;
	}

	return GKT_SUCCESS;
}

int user_dynpwd_EDTQ_verify(user_dynpwd_data_s *data,
				uint32_t user_index)
{
	const user_dynpwd_item_s *c_item;
	gkt_date_time_s date_time;
	uint32_t item_index;
	int retval;

	gkt_info("user_dynpwd_verify: <EDTQ> (%d) (%04u-%02u-%02u) (%02u:%02u)\n", 
		data->cancel, data->edtq.expire_date.year, 
		data->edtq.expire_date.month, data->edtq.expire_date.day,
		data->edtq.st_hour, data->edtq.et_hour);

	item_index = 0;
	while (item_index < g_user_dynpwd.instance.item_available_nums) {
		c_item = USER_DYNPWD_ITEM_C(item_index);
		if ((user_index == (uint32_t)c_item->ext_header.user_index)
			&& (USER_DYNPWD_TYPE_EDTQ == c_item->ext_header.dynpwd_type)
			&& (USER_ITEM_ST_CANCELLED == c_item->ext_header.state)
			&& (data->edtq.st_hour == c_item->edtq.st_hour)
			&& (data->edtq.et_hour == c_item->edtq.et_hour)
			&& (data->edtq.expire_date.u32_value == c_item->edtq.expire_date))
		{
			gkt_info("user_dynpwd_verify: <EDTQ> (%u) ALREADY CANCELLED\n", item_index);
			return GKT_USER_VERIFY_FAILED;
		}

		item_index++;
	}

	if (!data->cancel) {
		retval = gkt_rtc_get_time(&date_time);
		if (GKT_SUCCESS == retval) {
			if (date_time.date.u32_value <= data->edtq.expire_date.u32_value) {
				if ((date_time.time.hour >= data->edtq.st_hour)
					&& (date_time.time.hour < data->edtq.et_hour))
				{
					return GKT_USER_VERIFY_PASSED;
				}
				else
					return GKT_USER_VERIFY_INVALID_ACCESS_TIME;
			}
		}
	}
	else {
		gkt_trace("user_dynpwd_verify: <EDTQ> *** CANCEL ***\n");

		g_user_dynpwd.save_item.ext_header.user_index = user_index;
		g_user_dynpwd.save_item.ext_header.dynpwd_type = USER_DYNPWD_TYPE_EDTQ;
		g_user_dynpwd.save_item.edtq.st_hour = data->edtq.st_hour;
		g_user_dynpwd.save_item.edtq.et_hour = data->edtq.et_hour;
		g_user_dynpwd.save_item.edtq.expire_date = data->edtq.expire_date.u32_value;
		user_dynpwd_save_item(&g_user_dynpwd.save_item, USER_ITEM_ST_CANCELLED);

		return GKT_USER_VERIFY_CANCELLED;
	}

	return GKT_USER_VERIFY_FAILED;
}

#endif

